package com.hylanda.pb.controller;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.hylanda.pb.entity.ReportFile;
import com.hylanda.pb.model.ReportFileQo;
import com.hylanda.pb.service.ReportFileService;

/**
 * @author zhangy
 * @E-mail:blackoon88@gmail.com
 * @qq:846579287
 * @version created at：2018年2月6日 上午10:37:22 note
 */
@Controller
@RequestMapping("/reportfile")
public class ReportFileController {
	private static Logger logger = LoggerFactory.getLogger(ReportFileController.class);
	private static final String prefix = "pb/";
	@Autowired
	private ReportFileService reportFileService;

	@RequestMapping("/index")
	public String index() throws Exception {
		return prefix + "reportfile/index";
	}

	@RequestMapping("/new")
	public String create(ModelMap model, ReportFile reportFile) {
		return prefix + "reportfile/new";
	}
//	@RequestMapping(value = "/edit/{id}")
//	public String update(ModelMap model, @PathVariable Long id) {
//		ReportFile reportFile = reportFileService.findById(id);
//		model.addAttribute("reportFile",reportFile);
//		return prefix + "reportfile/edit";
//	}
	
//	@RequestMapping(value = "/{id}")
//	public String show(ModelMap model, @PathVariable Long id) {
//		ReportFile reportFile = reportFileService.findById(id);
//		model.addAttribute("reportFile",reportFile);
//		return prefix + "reportfile/show";
//	}
	
	@RequestMapping(value = "/list")
	@ResponseBody
	public Page<ReportFile> getList(ReportFileQo reportFileQo) {
		return reportFileService.findPage(reportFileQo);
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public String save(HttpServletRequest request,ReportFile reportFile,MultipartFile file) throws Exception {
		String fileName=file.getOriginalFilename();
		String OldfileName=reportFileService.findByName(fileName);
		if(OldfileName==null){
			reportFile.setFilename(fileName);
			reportFile.setDate(new Timestamp(System.currentTimeMillis()));
			reportFile.setFilecontent(file.getBytes());
			reportFile=reportFileService.create(reportFile);
			logger.info("新增->ID=" + reportFile.getId());
			return "1";
		}
		else{
			return "报表名已存在，请更改上传的报表名称";
		}
	}

//	@RequestMapping(method = RequestMethod.POST, value = "/update")
//	@ResponseBody
//	public String update(ReportFile reportFile) throws Exception {
//		reportFileService.update(reportFile);
//		logger.info("修改->ID=" + reportFile.getId());
//		return "1";
//	}

	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	@ResponseBody
	public String delete(@PathVariable Long id) throws Exception {
		int rownum=reportFileService.delete(id);
		if(rownum==1){
			logger.info("删除->ID=" + id);
			return "1";
		}else{
			return "删除操作失败";
		}
		
	}

}
